package com.example.springboot01.controller;

import com.example.springboot01.entity.Course;
import com.example.springboot01.req.PageQueryReq;
import com.example.springboot01.resp.Response;
import com.example.springboot01.resp.enums.CommonRespEnum;
import com.example.springboot01.resp.enums.ProfessorRespEnum;
import com.example.springboot01.service.CourseService;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/api/v1/course")
public class CourseController {


    @Resource
    private CourseService courseService;


    @PostMapping
    @ApiOperation(value = "添加课程")
    public Response add(@RequestBody Course course){
        courseService.save(course);
        return Response.ok(null);
    }

    @PutMapping
    @ApiOperation(value = "更新课程")
    public Response update(@RequestBody Course course){
        courseService.updateById(course);
        return Response.ok(null).setMsg("更新成功");
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除课程信息")
    public Response delete(@PathVariable Integer id){
        if (courseService.removeById(id)) {
            Response.response(ProfessorRespEnum.DELETE_ERROR);
        }
        return Response.response(CommonRespEnum.DELETE_OK);
    }

    @GetMapping
    @ApiOperation(value = "查询课程列表")
    public Response list(){
        return Response.ok(courseService.list());
    }

    @GetMapping("/page")
    public Response page(PageQueryReq req){
        return Response.ok(courseService.getPage(req));
    }


}
